package com.caterina.activities;

import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import transaction.Transaction;

import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MTransaction;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;
import com.caterina.components.COptionPane;

import controllers.ManagerTask.TaskType;

public class TransactionsActivity extends CActivity implements ModuleChangedListener{
	public TransactionsActivity(Container parent) {
		super("TRANSACTION_ACTIVITY",parent);
	}
	@Override
	public void onStart() {
		this.putTask(TaskType.TRANSACTION_GET_ALL, "");
		this.createButtons();
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case TRANSACTION_GET_ALL:{
				this.lastData=(Collection<Transaction>) data;
				this.createViewer();
				break;
			}
			case TRANSACTION_SEND:{
				this.createAddModule();
				this.collectionView.setModifyModeEnabled(false);
				this.putTask(TaskType.TRANSACTION_GET_ALL, "");
				break;
			}
		default:break;
		}
	}
	private void createAddModule(){
		this.addModule=new MTransaction(null, "NEW_TRANSACTION", "NEW_TRANSACTION_DESC", ViewType.WRITABLE);
		this.addModule.addWritableButton(save,cancel);
	}
	@Override
	public void onOptionPaneClosed(String status) {
		if (status==COptionPane.OK)
			this.putTask(TaskType.TRANSACTION_GET_ALL, "");
	}
	private void createButtons(){
		this.save=new CButton("TRANSACTION_SAVE") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				showCOPtionPane(new TransactionConfirmationActivity(addModule.getContent(),TransactionsActivity.this));
			}
			private static final long serialVersionUID = 1L;
		};
		this.cancel=new CButton("TRANSACTION_CANCEL") {
			@Override
			public void onFocus() {}
			@Override
			public void onAction() {
				createViewer();
			}
			private static final long serialVersionUID = 1L;
		};
	}
	private void createViewer(){
		if (this.collectionView!=null){
			this.collectionView.removeModuleChangedListener(this);
		}
		this.createAddModule();
		ArrayList<Module<?>> srvModules=new ArrayList<Module<?>>();
		for (Transaction trn: lastData){
			MTransaction trMod= new MTransaction(null, trn,"TRANSACTION", "TRANSACTION", ViewType.READ_ONLY);
			srvModules.add(trMod);
		}
		this.collectionView=new ModCollectionViewer(null,srvModules ,addModule, "TRANSACTION_LIST", "TRANSACTION_LIST_DESC", ViewType.WRITABLE,ModCollectionViewer.HIDE_REMOVE);
		this.collectionView.addModuleChangedListener(this);
		this.add(collectionView);
	}
	@Override
	public void onError(TaskType task, Exception e) {		
	}	
	@Override
	public void onModuleChange(Module<?> mod) {
		this.add(collectionView);
	}
	private CButton save;
	private CButton cancel;
	private Collection<Transaction> lastData;
	private ModCollectionViewer collectionView;
	private MTransaction addModule;
	private static final long serialVersionUID = 1L;
}
